package com.junzhiit.nengji.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.dao.BenefitSettingDAO;
import com.junzhiit.nengji.model.BenefitSetting;

@Repository
public class BenefitSettingDAOImpl extends AbstractBaseDAO<BenefitSetting> implements BenefitSettingDAO{

	@Override
	public List<BenefitSetting> findAll(BenefitSetting benefitSetting, int pageNumber, int maxPageRow) {
		StringBuffer sql=new StringBuffer();
		sql.append(" select * from benefit_setting");
		sql.append(" where is_delete=?");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		
		sql.append(" order by start_benefit asc");
		
		return super.queryForPagingList(sql.toString(), BenefitSetting.class, param, pageNumber, maxPageRow);
	}
	
	@Override
	public int getTotalCount(BenefitSetting benefitSetting) {
		StringBuffer sql=new StringBuffer();
		sql.append(" select count(*) from benefit_setting");
		sql.append(" where is_delete=?");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		
		sql.append(" order by start_benefit asc");
		
		return super.queryForInt(sql.toString(),param);
	}

	@Override
	public Double getBenefitMax() {
		StringBuffer sql=new StringBuffer();
		sql.append(" select ifnull(max(end_benefit),0) from benefit_setting");
		sql.append(" where is_delete=?");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		
		return super.queryForDouble(sql.toString(),param);
	}

	@Override
	public BenefitSetting findNextByBenefit(String id) {
		StringBuffer sql=new StringBuffer();
		sql.append(" select * from benefit_setting");
		sql.append(" where start_benefit > (select start_benefit from benefit_setting where id=?)");
		sql.append(" and is_delete=?");
		sql.append(" order by start_benefit asc LIMIT 0,1");
		
		StatementParameter param=new StatementParameter();
		param.setString(id);
		param.setInt(IsDelete.NO.toIntValue());
		
		return query(sql.toString(), BenefitSetting.class, param);
		
	}

	@Override
	public BenefitSetting findPrevByBenefit(String id) {
		StringBuffer sql=new StringBuffer();
		sql.append(" select * from benefit_setting");
		sql.append(" where start_benefit < (select start_benefit from benefit_setting where id=?)");
		sql.append(" and is_delete=?");
		sql.append(" order by start_benefit desc LIMIT 0,1");
		
		StatementParameter param=new StatementParameter();
		param.setString(id);
		param.setInt(IsDelete.NO.toIntValue());
		
		return query(sql.toString(), BenefitSetting.class, param);
	}

	@Override
	public BenefitSetting findBenefit(Double profit) {
		StringBuffer sql = new StringBuffer();
		sql.append("select * from benefit_setting  where ?>=start_benefit and ?<end_benefit and is_delete = ?");
		StatementParameter params = new StatementParameter();
		params.setDouble(profit);
		params.setDouble(profit);
		params.setInt(IsDelete.NO.toIntValue());
		return query(sql.toString(), BenefitSetting.class, params);
	}


}
